package main.java.cn.wolfcode.web.controller;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionContorler {
    @Autowired
    private IPermissionService permissionService;

    @RequiredPermission(name="权限页面",expression="permission:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }
    @RequiredPermission(name = "权限加载",expression = "permission:reload")
    @ResponseBody
    @RequestMapping("/reload")
    public JsonResult reload() {
        try {
            permissionService.reload();
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(true);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "权限加载失败");
        }

    }
}
